package com.cloudptt.wp.ui.message.component;

import java.io.File;

import com.cloudptt.api.comm.Common;
import com.cloudptt.api.product.log.Log;
import com.mcptt.comm.CommonFunc;

import android.media.MediaRecorder;
import android.os.Environment;

/**
 * Created by dell on 2018/4/17.
 */

public class AudioRecorder 
{
	private static final String THIS_FILE = "AudioRecorder";
    private MediaRecorder mMediaRecorder;
    private static final String Sms_File_Path = Environment.getExternalStorageDirectory() + File.separator + "CloudPTT" + File.separator + "SMS" + File.separator;// + "REC" + File.separator;
    private static final int MaxLevel = 7;
    private String mCurrentFilePath;// 保存音频文件的全路径
    private boolean isPrepared = false;// 是否准备完毕
    private static AudioRecorder mInstance;

    private AudioRecorder() {
    	Log.d(THIS_FILE, "AudioRecorder Sms_File_Path = " + Sms_File_Path);
    }
    public static AudioRecorder getmInstance() {
        if (mInstance == null) {
            synchronized (AudioRecorder.class) {
                if (mInstance == null) {
                    mInstance = new AudioRecorder();
                }
            }
        }
        return mInstance;
    }

    /**
     * 准备完毕的回调
     */
    public interface AudioStateListener {
        void wellPrepared();
    }

    private AudioStateListener mListener;

    public void setAudioStateListener(AudioStateListener listener) {
        mListener = listener;
    }
    /** 准备录制 */
    public void prepareAudio() 
    {
        try 
        {
        	Log.d(THIS_FILE, "prepareAudio");
            isPrepared = false;
            String path = Sms_File_Path + Common.getDate() + File.separator + "REC" + File.separator;
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            Log.d(THIS_FILE, "prepareAudio 1");

            String fileName = generateName();
            Log.d(THIS_FILE, "prepareAudio fileName = " + fileName);
            File file = new File(dir, fileName);
            mCurrentFilePath = file.getAbsolutePath();
            mMediaRecorder = new MediaRecorder();
            Log.d(THIS_FILE, "prepareAudio 2");
            // 设置输出文件
            mMediaRecorder.setOutputFile(mCurrentFilePath);
            // 设置音频源为麦克风
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            // 设置音频格式
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
            // 设置音频编码
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            Log.d(THIS_FILE, "prepareAudio 3");
            
            mMediaRecorder.prepare();
            mMediaRecorder.start();
            
            Log.d(THIS_FILE, "prepareAudio 4");

            isPrepared = true;
            if (mListener != null) {
                mListener.wellPrepared();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(THIS_FILE, "prepareAudio e = " + e);
        }
    }

    /** 获取音量大小 */
    public int getVoiceLevel() {
        if (isPrepared)
        {
            try
            {
                //mMediaRecorder.getMaxAmplitude() 1-32767
                //注意此处mMediaRecorder.getMaxAmplitude 只能取一次，如果前面取了一次，后边再取就为0了
                if(mMediaRecorder != null)
                {
                    int val = mMediaRecorder.getMaxAmplitude();
                    Log.d(THIS_FILE, "getVoiceLevel F val = " + val);
                    val = val * MaxLevel / 32768 + 1;
                    Log.d(THIS_FILE, "getVoiceLevel val = " + val);
                    return val;
                }
                else
                {
                    Log.d(THIS_FILE, "mMediaRecorder = null");
                }
                // return ((mMediaRecorder.getMaxAmplitude() * maxLevel) / 32768) + 1;
            } catch (Exception e)
            {
                e.printStackTrace();
                Log.d(THIS_FILE, "getVoiceLevel e = " + e);
            }

        }
        return 1;
    }

    public static int normalizeTo7Range(double value, double min, double max) {
        return (int) Math.round(7 * (value - min) / (max - min));
    }


    /** 保存录音，释放资源 */
    public void release() 
    {
    	Log.d(THIS_FILE, "release");
        if(mMediaRecorder != null) {
            mMediaRecorder.stop();
            mMediaRecorder.release();
            mMediaRecorder = null;
        }
    }

    /** 取消录制 */
    public void cancel() 
    {
    	Log.d(THIS_FILE, "cancel mCurrentFilePath = " + mCurrentFilePath);
        release();
        if(mCurrentFilePath != null) 
        {
            File file = new File(mCurrentFilePath);
            if(file.exists()) {
                file.delete();
                mCurrentFilePath = null;
            }
        }
    }

    /** 获取录制音频的总路径 */
    public String getmCurrentFilePath(){
        return mCurrentFilePath;
    }

    /**
     * 生成一个随机名字
     */
    private String generateName() {
    	return CommonFunc.getDateAndTime2() + ".amr";
    }
}
